import 'package:drift/drift.dart';
import 'package:drift_flutter/drift_flutter.dart';
import 'package:path_provider/path_provider.dart';

part 'database.g.dart';

class User extends Table {
  IntColumn get id => integer().autoIncrement()();
  // TextColumn get email => text().withLength(min: 6, max: 32)();
  TextColumn get email => text()();
  TextColumn get phone => text()();
  TextColumn get username => text()();
  TextColumn get header_icon => text()();
  TextColumn get access_token => text()();
  TextColumn get refresh_token => text()();
  IntColumn get expired_time => integer()();
  DateTimeColumn get create_time => dateTime()();
  DateTimeColumn get update_time => dateTime()();
}


class Devices extends Table {
  IntColumn get id => integer().autoIncrement()();
  // TextColumn get email => text().withLength(min: 6, max: 32)();
  IntColumn get user_id => integer()();
  TextColumn get mac => text()();
  TextColumn get d_name => text()();
  TextColumn get battery => text()();
  TextColumn get hw_version => text()();
  DateTimeColumn get create_time => dateTime()();
  DateTimeColumn get update_time => dateTime()();
}

class TranslationHistory extends Table {
  IntColumn get id => integer().autoIncrement()();
  // TextColumn get email => text().withLength(min: 6, max: 32)();
  IntColumn get user_id => integer()();
  TextColumn get src_lang => text()();
  TextColumn get tgt_lang => text()();
  TextColumn get src_lang_display => text()();
  TextColumn get tgt_lang_display => text()();
  TextColumn get raw_text => text()();
  TextColumn get translation => text()();
  TextColumn get taskId => text()();
  IntColumn get dataType => integer()();
  DateTimeColumn get create_time => dateTime()();
  DateTimeColumn get update_time => dateTime()();
}

class Sessions extends Table {
  IntColumn get id => integer().autoIncrement()();
  // TextColumn get email => text().withLength(min: 6, max: 32)();
  IntColumn get user_id => integer()();
  IntColumn get folder_id => integer()();
  IntColumn get  audio_length => integer()();
  TextColumn get session_name => text()();
  TextColumn get s_name => text()(); //来源名称
  TextColumn get s_type => text()(); //来源类型.
  BoolColumn get has_read => boolean()();
  TextColumn get audio_path => text()();
  TextColumn get audio_size => text()();
  DateTimeColumn get create_time => dateTime()();
  DateTimeColumn get update_time => dateTime()();
}


class Folder extends Table {
  IntColumn get id => integer().autoIncrement()();
  // TextColumn get email => text().withLength(min: 6, max: 32)();
  IntColumn get user_id => integer()();
  TextColumn get f_name => text()();
  DateTimeColumn get create_time => dateTime()();
  DateTimeColumn get update_time => dateTime()();
}

class Settings extends Table {
  IntColumn get id => integer().autoIncrement()();
  // TextColumn get email => text().withLength(min: 6, max: 32)();
  IntColumn get user_id => integer()();
  BoolColumn get off_line => boolean()();
  BoolColumn get only_show_trans_text => boolean()();
  BoolColumn get auto_play => boolean()();
  BoolColumn get allowOtherInput => boolean()();
  IntColumn  get voiceType => integer()();
  IntColumn  get speechSpeed => integer()();
  DateTimeColumn get create_time => dateTime()();
  DateTimeColumn get update_time => dateTime()();
}


class Summerize extends Table {
  IntColumn get id => integer().autoIncrement()();
  // TextColumn get email => text().withLength(min: 6, max: 32)();
  IntColumn get session_id => integer()();
  IntColumn get m_id => integer()();
  BlobColumn get s_content => blob()();
  DateTimeColumn get create_time => dateTime()();
  DateTimeColumn get update_time => dateTime()();
}

class AudioTranscript extends Table {
  IntColumn get id => integer().autoIncrement()();
  // TextColumn get email => text().withLength(min: 6, max: 32)();
  IntColumn get session_id => integer()();
  IntColumn get chat_sequence => integer()();
  IntColumn get m_id => integer()();
  TextColumn get a_text => text()();
  IntColumn get duration => integer()();
  IntColumn get offset => integer()();
  TextColumn get speaker => text()();
  DateTimeColumn get create_time => dateTime()();
  DateTimeColumn get update_time => dateTime()();
}

class AIQaA extends Table {
  IntColumn get id => integer().autoIncrement()();
  // TextColumn get email => text().withLength(min: 6, max: 32)();
  IntColumn get session_id => integer()();
  IntColumn get m_id => integer()();
  BlobColumn get a_content => blob()();
  DateTimeColumn get create_time => dateTime()();
  DateTimeColumn get update_time => dateTime()();
}

class Models extends Table {
  IntColumn get id => integer().autoIncrement()();
  // TextColumn get email => text().withLength(min: 6, max: 32)();
  TextColumn get m_name => text()();
  TextColumn get m_produce => text()();
  TextColumn get url => text()();
  DateTimeColumn get create_time => dateTime()();
  DateTimeColumn get update_time => dateTime()();
}

// {"action":"result","text":"根据实际需求选择解决方案。","finish":true,"duration":2560,"offset":76370,"speaker":"Guest-1","code":200}

@DriftDatabase(tables: [User,Devices,TranslationHistory,Sessions,Folder,
  Settings,Summerize,AudioTranscript,AIQaA,Models])
class AppDatabase extends _$AppDatabase {

  AppDatabase([QueryExecutor? executor]) : super(executor ?? _openConnection());

  @override
  int get schemaVersion => 1;

  static QueryExecutor _openConnection() {
    return driftDatabase(
      name: 'zhiyi_database',
      native: const DriftNativeOptions(
        // By default, `driftDatabase` from `package:drift_flutter` stores the
        // database files in `getApplicationDocumentsDirectory()`.
        databaseDirectory: getApplicationSupportDirectory,
      ),
      // If you need web support, see https://drift.simonbinder.eu/platforms/web/
    );
  }

}